package com.jinkaimen.kqb.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.LocationBean;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.ui.adapter.LocationListAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class SelectLocationActivity extends BaseActivity {

    public static final int REQUEST_CODE_SELECT_LOCATION_CITY = 100;
    private static final String CITY_ID = "city_id";
    public static final String PARENT_ID = "parent_id";
    public static final String TYPE = "type";
    public static final String CURRENT_LOCATION = "current_location";
    public static final String BANK_LOCATION = "bank_location";

    private ListView mListView;
    private List<LocationBean> mData;
    private LocationListAdapter mAdapter;
    private String mCurrentLocation;
    private String mParentCode;
    private LocationBean mSelectLocation;
    private String type = "";

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mCurrentLocation = callIntent.getStringExtra(CURRENT_LOCATION);
        mParentCode = callIntent.getStringExtra(PARENT_ID);
        type = callIntent.getStringExtra(TYPE);
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("选择地区");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_select_location;
    }

    @Override
    protected void initChildView() {
        mListView = (ListView) findViewById(R.id.lv_activity_select_location);
        if (mParentCode == null) {
//            View headerView = View.inflate(this, R.layout.header_activity_select_location, null);
//
//            TextView selectedLocationView = (TextView) headerView.findViewById(R.id.tv_header_activity_select_location_selected_location);
//            selectedLocationView.setText(TextUtils.isEmpty(mCurrentLocation)?"请选择":mCurrentLocation);
//            mListView.addHeaderView(headerView);
        }
    }

    @Override
    protected void initChildData() {

        if ("city".equals(type)) {
            type = "country";
        }

        if ("province".equals(type)) {
            type = "city";
        }

        if (mParentCode == null) {
            mParentCode = "0";
            //province|city|country;
            type = "province";
        }

        getCityListFromNet(mParentCode);
    }

    private void getCityListFromNet(String parentCode) {
        ApiManager.getApi().getCityList(
                "HSS001038",
                parentCode,
                type
        ).enqueue(new NetCallBack<List<LocationBean>>() {
            @Override
            protected void onSuccess(Call<List<LocationBean>> call, Response<List<LocationBean>> response) {
                mData = response.body();
                mAdapter = new LocationListAdapter(SelectLocationActivity.this, mData);
                mListView.setAdapter(mAdapter);
            }
        });
    }

    @Override
    protected void setChildViewListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                int index = position;
                if ("0".equals(mParentCode)) {
//                    index--;
                }

                if (index < 0 || index > mData.size() - 1) {
                    return;
                }

                LocationBean locationBean = mData.get(index);
//                if (!locationBean.isLeaf) {
                if (!"country".equals(type)) {

                    mSelectLocation = locationBean;

                    Bundle bundle = new Bundle();
                    bundle.putString(PARENT_ID, locationBean.code);
                    bundle.putString(TYPE, type);
                    if ("city".equals(type)) {
                        SharedPreferencesUtils.put("bankUseCode", locationBean.code);
                        SharedPreferencesUtils.put("city_code", locationBean.code);
                        SharedPreferencesUtils.put("city_aname", locationBean.aname);
                    }
                    if ("province".equals(type)) {
                        SharedPreferencesUtils.put("province_code", locationBean.code);
                        SharedPreferencesUtils.put("province_aname", locationBean.aname);
                    }

                    ActivityUtils.startActivityForResult(SelectLocationActivity.this, SelectLocationActivity.class, bundle, REQUEST_CODE_SELECT_LOCATION_CITY);
                } else {
                    SharedPreferencesUtils.put("country_code", locationBean.code);
                    SharedPreferencesUtils.put("country_aname", locationBean.aname);
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("cityCode", locationBean.code);
                    returnIntent.putExtra("areaName", locationBean.aname);
                    returnIntent.putExtra("branchDistrictCode", locationBean.code);
                    setResult(RESULT_OK, returnIntent);
                    ActivityUtils.finishActivity(SelectLocationActivity.this);
                }

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_SELECT_LOCATION_CITY:
                    String cityName = data.getStringExtra("cityName");

                    if (cityName == null) {
                        data.putExtra("cityName", mSelectLocation.aname);
                    }

                    setResult(RESULT_OK, data);
                    ActivityUtils.finishActivity(this);
                    break;
            }
        }
    }
}
